package com.cn.wanxi.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public class LoadFile {
    public static void load(HttpServletRequest req) throws IOException {
        //       临时缓存
        DiskFileItemFactory factory = new DiskFileItemFactory();
//        解析文件
        ServletFileUpload upload = new ServletFileUpload(factory);
//        设置文件的大小
        factory.setSizeThreshold(1024 * 1024);
        List items = null;
        try {
//            将req里面的所有数据强制转换为List<FileItem>文件集合
            items = upload.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
//        Iterator和List区别？线程安全和线程非安全的区别
        Iterator iter = items.iterator();
        String filename = "";
        String derectory = "/loadImg/";
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
//            判断是否是表单数据item.isFormField()
            if (!item.isFormField()) {
                // 根据时间戳创建头像文件
                filename = System.currentTimeMillis() + "---" + item.getName();
                String servletPath = req.getServletContext().getRealPath("/");
                System.out.println(servletPath);
//                String path="E:\wanxi\step-4\canyin04-0329\out\artifacts\canyin04_0329_war_exploded\";
                /*File f = new File(getServletContext().getRealPath("upload"));*/
                File f = new File(servletPath.split("out")[0] + "/web/" + derectory);
                if (!f.exists()) {
                    f.mkdir();
                }
                String imgsrc = f + "/" + filename;
                // 复制文件
                InputStream is = item.getInputStream();
                FileOutputStream fos = new FileOutputStream(imgsrc);
                FileOutputStream out = new FileOutputStream(servletPath + derectory + filename);
                byte b[] = new byte[1024 * 1024];
                int length = 0;
                while (-1 != (length = is.read(b))) {
                    fos.write(b, 0, length);
                    out.write(b, 0, length);
                }
                fos.flush();
                fos.close();
            } else {
                System.out.println(item.getFieldName());
                String value = item.getString();
if(item.getFieldName().equals("loginName")){
    //            在session里面去查询是否有这个用户名
    String loginName = (String) req.getSession().getAttribute(value);
    if (loginName == null) {
 req.getSession().setAttribute("resultCode",-1);
    } else {
//                    filterChain.doFilter(req, resp);//放行
    }
}





                value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                System.out.println(value);
            }
            req.getSession().setAttribute("imgHref", derectory + filename);
        }
    }
}
